package com.avic.service.impl;

import com.avic.common.util.AssertUtil;
import com.avic.common.util.ex.BizErrorCode;
import com.avic.common.util.ex.BizException;
import com.avic.dal.condition.PageQueryShipResourceListCondition;
import com.avic.dal.model.ShipResource;
import com.avic.service.ShipResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author: huangsheng
 * @date: 2018/9/4
 */
@Service
@Slf4j
public class ShipResourceServiceImpl extends AbstractServiceImpl implements ShipResourceService {

    /**
     * 分页查询列表
     *
     * @see ShipResourceService#itemList(PageQueryShipResourceListCondition)
     */
    @Override
    public List<ShipResource> itemList(PageQueryShipResourceListCondition pageQueryShipResourceListCondition) {
        return shipResourceMapper.itemList(pageQueryShipResourceListCondition);
    }

    /**
     * 查询数据总数
     *
     * @see ShipResourceService#queryRecordsTotal(PageQueryShipResourceListCondition)
     */
    @Override
    public int queryRecordsTotal(PageQueryShipResourceListCondition pageQueryShipResourceListCondition) {
        return shipResourceMapper.queryRecordsTotal(pageQueryShipResourceListCondition);
    }

    /**
     * 新增船舶资源
     *
     * @see ShipResourceService#create(ShipResource)
     */
    @Override
    public int create(ShipResource shipResource) {

        AssertUtil.isTrue(shipResourceMapper.queryItemCountByShipNumber(shipResource.getShipNumber()) <= 0,
                BizErrorCode.PARAM_ERROR, "船舶编号【" + shipResource.getShipNumber() + "】已经存在");
        try {
            shipResourceMapper.insert(shipResource);
            log.info("创建船舶资源 {}", shipResource.getId());
            return shipResource.getId();
        } catch (DuplicateKeyException de) {
            log.warn("并发创建船舶资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "船舶编号【" + shipResource.getShipNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("创建船舶资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "创建船舶资源发生错误");
        }
    }

    /**
     * 更新船舶资源
     *
     * @see ShipResourceService#update(ShipResource)
     */
    @Override
    public void update(ShipResource shipResource) {
        log.info("修改船舶资源 {}", shipResource.getId());

        ShipResource dbShipResource = shipResourceMapper.queryItemById(shipResource.getId());
        AssertUtil.isTrue(null != dbShipResource, BizErrorCode.PARAM_ERROR, "不存在此数据");

        if (!StringUtils.equals(dbShipResource.getShipNumber(), shipResource.getShipNumber())) {
            AssertUtil.isTrue(shipResourceMapper.queryItemCountByShipNumber(shipResource.getShipNumber()) <= 0,
                    BizErrorCode.PARAM_ERROR, "船舶编号【" + shipResource.getShipNumber() + "】已经存在");
        }
        try {
            shipResourceMapper.update(shipResource);
        } catch (DuplicateKeyException de) {
            log.warn("并发修改船舶资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "船舶编号【" + shipResource.getShipNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("修改船舶资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "修改船舶资源发生错误");
        }
    }

    /**
     * 删除船舶资源
     *
     * @see ShipResourceService#delete(int)
     */
    @Override
    public void delete(int id) {
        try {
            log.info("删除船舶资源 {}", id);
            shipResourceMapper.delete(id);
        } catch (Exception e) {
            log.error("删除船舶资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "删除船舶资源的时候发生错误");
        }
    }
}
